package hit.example.home;

import hit.example.Db.DbRecentFav;
import hit.example.Object.ObjWord;
import hit.example.adapter.AdapterListView;
import hit.example.hitdict.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.zip.Inflater;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class RecentFabvoriteFm extends MyFragment implements
		OnItemClickListener, OnClickListener {

	private int type;
	DbRecentFav db;
	AdapterListView adp;
	View v;
	Button del;
	ListView lv;
	TextView txtInfo,txtMarqueHitDict;
	ObjWord objW = null;
	ArrayList<ObjWord> listWord;

	private RecentFabvoriteFm() {
		// TODO Auto-generated constructor stub
	}

	public static RecentFabvoriteFm getInstance(int type) {
		RecentFabvoriteFm fm = new RecentFabvoriteFm();
		fm.setType(type);
		return fm;
	}
	protected void setType(int type) {
		this.type = type;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fm_recent_fav, container, false);
		lv = (ListView) v.findViewById(R.id.lvRecentFav);
		del = (Button) v.findViewById(R.id.btnDelAll);
		del.setOnClickListener(this);

		txtMarqueHitDict=(TextView)v.findViewById(R.id.txtMarque);
		txtMarqueHitDict.setSelected(true);
		txtInfo = (TextView) v.findViewById(R.id.txtInfo);
		HomeActivity.getInstance().setFont(txtInfo, getActivity());
		HomeActivity.getInstance().setHide();

		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (type == 0) { // recent
			HomeActivity.getInstance().changeTitle("Recent Word");
			db = new DbRecentFav(getActivity());
			listWord = db.getlistRecent();
			adp = new AdapterListView(getActivity(),
					R.layout.custom_listview_item, listWord);
			lv.setAdapter(adp);
			lv.setOnItemClickListener(this);

		}

		else if (type == 1){
			HomeActivity.getInstance().changeTitle("Favorite Word");
			db = new DbRecentFav(getActivity());
			listWord = db.getlistFavor();
			adp = new AdapterListView(getActivity(),
					R.layout.custom_listview_item, listWord);
			lv.setAdapter(adp);
			lv.setOnItemClickListener(this);
		}
		registerForContextMenu(lv);
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int pos, long arg3) {
				objW = listWord.get(pos);
				return false;
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getActivity().getMenuInflater().inflate(
				R.menu.contextmenu_list_recent_fav, menu);

	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnXoaWord:
			DeleteWord();
			break;

		}
		return super.onContextItemSelected(item);
	}

	public void DeleteWord() {
		db = new DbRecentFav(getActivity());
		db.delThisWordRecent(objW);
		adp.notifyDataSetChanged();
		adp = new AdapterListView(getActivity(),
				R.layout.custom_listview_item, listWord);
		lv.setAdapter(adp);
		showToast(objW.getiWord()+" is Deleted");
	}

	@Override
	public boolean onBackPressed() {
		HomeActivity.getInstance().setUptitle("English - Vietnamese");
		HomeActivity.getInstance().setShow();
		HomeActivity.getInstance().pusListWordFm();
		
				return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		if (type == 0) {
			HomeActivity.getInstance().pushDetail(adp.getItem(pos),type);
		} else if (type == 1){
			HomeActivity.getInstance().pushDetail(adp.getItem(pos),type);
		}
	}

	@Override
	public void onClick(View v) {
		if (type == 0) {
			switch (v.getId()) {
			case R.id.btnDelAll:
				db = new DbRecentFav(getActivity());
				db.delAllRecentWord();
				adp.notifyDataSetChanged();
				adp = new AdapterListView(getActivity(),
						R.layout.custom_listview_item, db.getlistRecent());
				lv.setAdapter(adp);
				showToast("Recent Word Deleted All ");
				break;
			}
		} else if (type == 1) {
			switch (v.getId()) {
			case R.id.btnDelAll:
				db = new DbRecentFav(getActivity());
				db.delAllFavorWord();
				adp.notifyDataSetChanged();
				adp = new AdapterListView(getActivity(),
						R.layout.custom_listview_item, db.getlistFavor());
				lv.setAdapter(adp);
				showToast("Favorite Word Deleted All");
				break;
			}
		}
	}
}
